package com.example.child_protection.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.child_protection.dao.CategoryAttrDao;
import com.example.child_protection.dto.info.CategoryAttrDTO;
import com.example.child_protection.dto.request.SimpleIdRequestDTO;
import com.example.child_protection.dto.request.find.CategoryAttrRequestDTO;
import com.example.child_protection.dto.response.CategoryAttrListResponseDTO;
import com.example.child_protection.dto.response.CategoryAttrResponseDTO;
import com.example.child_protection.enums.ServiceActionEnums;
import com.example.child_protection.model.CategoryAttrModel;
import com.example.child_protection.service.CategoryAttrQueryService;
import com.example.child_protection.service.base.MyBaseService;
import com.example.child_protection.service.base.MyServiceExecutor;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 类目属性查询service实现层
 *
 * @author Biz
 * @date 2018-11-2
 */
@Component
public class CategoryAttrQueryServiceImpl extends MyBaseService implements CategoryAttrQueryService {

    @Autowired
    public CategoryAttrDao categoryAttrDao;

    /**
     * id查询
     *
     * @param requestDTO
     * @return
     */
    public CategoryAttrResponseDTO findId(SimpleIdRequestDTO requestDTO) {
        return this.service(ServiceActionEnums.CATEGORY_ATTR_FIND_ID, requestDTO, CategoryAttrResponseDTO.class, new MyServiceExecutor<SimpleIdRequestDTO, CategoryAttrResponseDTO>() {
            @Override
            protected CategoryAttrResponseDTO myprocess(SimpleIdRequestDTO requestDTO) throws Exception {
                CategoryAttrResponseDTO responseDTO = new CategoryAttrResponseDTO();
                CategoryAttrModel categoryAttrModel = categoryAttrDao.findId(requestDTO.getId());
                CategoryAttrDTO categoryAttr = new CategoryAttrDTO();
                BeanUtils.copyProperties(categoryAttrModel, categoryAttr);
                responseDTO.setInfo(categoryAttr);
                responseDTO.successfully();
                return responseDTO;
            }
        });
    }

    /**
     * 列表查询
     *
     * @param requestDTO
     * @return
     */
    public CategoryAttrListResponseDTO findList(CategoryAttrRequestDTO requestDTO) {
        return this.service(ServiceActionEnums.CATEGORY_ATTR_FIND_LIST, requestDTO, CategoryAttrListResponseDTO.class, new MyServiceExecutor<CategoryAttrRequestDTO, CategoryAttrListResponseDTO>() {
            @Override
            protected CategoryAttrListResponseDTO myprocess(CategoryAttrRequestDTO requestDTO) throws Exception {
                CategoryAttrListResponseDTO responseDTO = new CategoryAttrListResponseDTO();
                CategoryAttrModel req = new CategoryAttrModel();
                BeanUtils.copyProperties(requestDTO, req);
                List<CategoryAttrModel> lists = categoryAttrDao.findPage(req);
                List<CategoryAttrDTO> roles = JSONObject.parseArray(JSONArray.toJSONString(lists), CategoryAttrDTO.class);
                responseDTO.setInfos(roles);
                responseDTO.successfully();
                return responseDTO;
            }
        });
    }
}
